if (!document.getElementById) {
    document.getElementById = function() { return null; }
}

function initMenu() {
    var uls = document.getElementsByTagName("ul");
    for (i = 0; i < uls.length; i++) {
        if (uls[i].className == "menuList") {
            decorateMenu(uls[i]);
            
        }
    }
}
function ajustRightTopBorder(menuListUl){
	var menuNum = menuListUl.getElementsByTagName("UL").length;
	var rightTopBorderWidth = 654 - (72*menuNum);
	jQuery('div[@class=rightTopBorder]').width(rightTopBorderWidth);
}
function decorateMenu(menu) {
    var items = menu.getElementsByTagName("div");
    
    activateMenu(items);
    ajustRightTopBorder(menu);
}

function activateMenu(items) {
    var activeMenu;
    var found = 0;
    for (var i=0; i < items.length; i++) {
    	if(items[i].firstChild.tagName == "A"){
	        var url = items[i].firstChild.getAttribute("href");
	        var current = document.location.toString();
	        if (url != null && url != "" && _getUrlIdentity(current) == _getUrlIdentity(url)) {
	            found++;
	        }
    	}
    }
     
    // more than one found, use cookies
    if (found > 1 || found == 0) {  
        var menuSelectedUrl = _getUrlIdentity(window.location.href); 
        var menuSelected = _getMenuSelectedIndex(menuSelectedUrl, items);
        if(menuSelected == null || menuSelected == -1){
        	menuSelectedUrl = getCookie("menuSelected"); 
        	menuSelected = _getMenuSelectedIndex(menuSelectedUrl, items);
        }
        if(menuSelected != null && menuSelected != -1){
	     	setCookie('menuSelected', menuSelectedUrl, null, "/");       	
	        if (items[menuSelected].parentNode.className == "submenu") {
	        	var parentMenu = _getParentMenu(items[menuSelected].parentNode);
	            parentMenu.className="selected";
	            items[menuSelected].parentNode.className="selected";
	            items[menuSelected].className="subSelected";
	        } else {      
	        	var subMenu = _getSubMenu(items[menuSelected]);
	        	var className = items[menuSelected].className;
	        	if(className.indexOf("selected") == -1){  
	            	items[menuSelected].className+="selected";
	            	if(subMenu != null){
	            		subMenu.className="selected";
	            	}
	            	var firstSubMenuItem = _getSubMenuFirstItem(subMenu);
	            	if(firstSubMenuItem != null && firstSubMenuItem.className.indexOf("subSelected") == -1){
	            		firstSubMenuItem.className += "subSelected";
	            		//firstSubMenuItem.parentNode.getElementsByTagName("DIV")[0].style.backgroundImage = 'url(images/ext/s.gif)';
	            	}
	        	}
	        }
        }
    } else{
        // only one found, match on URL
        for (var i=0; i < items.length; i++) {
        	if(items[i].firstChild.tagName == "A"){
	            var url = items[i].firstChild.getAttribute("href");
	            url = _getUrlIdentity(url);
	            var current = document.location.toString();
	            current =  _getUrlIdentity(current);
	            if (current == url) {
	            	setCookie('menuSelected', url, null, "/");
	            	var menuSelected = i;
		            //items[i].parentNode.getElementsByTagName("DIV")[0].style.backgroundImage = 'url(images/ext/s.gif)';
	                if (items[i].parentNode.className == "submenu") {
	                    
	                    var parentMenu = _getParentMenu(items[menuSelected].parentNode);
			            parentMenu.className="selected";
			            items[menuSelected].parentNode.className="selected";
			            items[menuSelected].className="subSelected";
	                } else {            
			        	var subMenu = _getSubMenu(items[menuSelected]);
			        	var className = items[menuSelected].className;
			        	if(className.indexOf("selected") == -1){  
			            	items[menuSelected].className+="selected";
			            	if(subMenu != null){
			            		subMenu.className="selected";
			            	}
			        	}
	                }
	            }
        	}
        }
    }
}
/**
 * 把url里面application context段替换掉
 * eg. 
 * 		'/elog/freights.html' -> 'freights.html'
 */
function _getUrlIdentity(url){
	var result = '';
	if(url != null){
		var regex1 = /^http:\/\/[^\/]*\//;
		result = url.replace(regex1, '/');
		var regex2 = /^\/[^\/]*\//;
		result = result.replace(regex2, '/');
		var regex3 = /\?.*$/;
		result = result.replace(regex3, '');
		var regex4 = /;jsessionid=.*$/;
		result = result.replace(regex4, '');
	}
	return result;
}
function _getMenuSelectedIndex(menuSelectedUrl, items){
	for (var i=0; i < items.length; i++) {
		if(items[i].firstChild.tagName == "A"){
			if(_getUrlIdentity(items[i].firstChild.getAttribute("href")) == menuSelectedUrl){
				return i;
			}else if(_getUrlIdentity(items[i].firstChild.getAttribute("href")) ==
						_getUrlIdentity(menuSelectedUrl)){
				return i;
			}
		}
	}
	return -1;
}
function _getSubMenu(menuLi){
	var result = menuLi.nextSibling;
	while(result != null && result.tagName != 'UL'){
		if(result.tagName == 'DIV'){
			result = null;			
			break;
		}
		result = result.nextSibling;
	}
	return result;
}
function _getParentMenu(menuUl){
	var result = menuUl.previousSibling;
	while(result.tagName != 'DIV'){
		if(result.tagName == 'UL'){
			result = null;			
			break;
		}
		result = result.previousSibling;
		if(result == null){
			break;
		}
	}
	return result;
}
function _getSubMenuFirstItem(menuUl){
	var result = null;
	if(menuUl == null){
		return result;
	}
	var childNodes = menuUl.childNodes;
	var childNode;
	for(var i=0; i<childNodes.length; i++){
		childNode = childNodes[i];
		if(childNode.tagName == 'DIV' && childNode.className == ''){
			result = childNode;
			break;
		}
	}
	return result;
}
// Select the menu that matches the URL when the page loads
window.onload=initMenu;

// =========================================================================
//                          Cookie functions 
// =========================================================================
/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
	var prefix = name + "=" 
	var start = document.cookie.indexOf(prefix) 

	if (start==-1) {
		return null;
	}
	
	var end = document.cookie.indexOf(";", start+prefix.length) 
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end) 
	return unescape(value);
}

/* This function is used to delete cookies */
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}